//
//  HMGetGoodsAdressTableViewController.m
//  G05爱鲜蜂
//
//  Created by 小灰灰 on 16/10/16.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMGetGoodsAdressTableViewController.h"
#import "HMGetGoodsAdressModel.h"
#import "HMAdressTableViewCell.h"
#import "HMAddAdressViewController.h"
static NSString *ide2 = @"HMGetGoodsAdressTableViewController";
@interface HMGetGoodsAdressTableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView *getGoodsAdressTableView;
@property(nonatomic,strong)NSMutableArray *modelArray;
@end

@implementation HMGetGoodsAdressTableViewController
-(NSArray *)modelArray{
    if(_modelArray == nil){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title = @"我的收获地址";
   NSString *path = [[NSBundle mainBundle]pathForResource:@"MyAdress" ofType:@"json"];
    //转二进制
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSArray *dataArray = dataDic[@"data"];
    for(int i = 0;i < dataArray.count;i++){
     HMGetGoodsAdressModel *model = [HMGetGoodsAdressModel yy_modelWithDictionary:dataArray[i]];
        [self.modelArray addObject:model];
    }
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _getGoodsAdressTableView = tableView;
    [self.view addSubview:tableView];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [UIView new];
    UINib *nib = [UINib nibWithNibName:@"HMAdressTableViewCell" bundle:nil];
    [tableView  registerNib:nib forCellReuseIdentifier:ide2];
    tableView.sectionHeaderHeight = 10;
    tableView.rowHeight = 65;
    //添加新增地址按钮
    UIButton *addAdress = [[UIButton alloc]init];
    [self.view addSubview:addAdress];
    [addAdress setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    [addAdress setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_selected"] forState:UIControlStateSelected];
    [addAdress setTitle:@"+  新增地址" forState:UIControlStateNormal];
    [addAdress setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addAdress addTarget:self action:@selector(addAdressBtn) forControlEvents:UIControlEventTouchUpInside];
    [addAdress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-10);
        make.width.equalTo(self.view).multipliedBy(0.7);
        make.centerX.equalTo(self.view);
        
    }];
}
//新增地址按钮

-(void)addAdressBtn{
    HMAddAdressViewController *addAdressVC = [[HMAddAdressViewController alloc] init];
    [self.navigationController pushViewController:addAdressVC animated:YES
     ];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMGetGoodsAdressModel *model = self.modelArray[indexPath.row];
    HMAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide2 forIndexPath:indexPath];
    
    cell.adressModel = model;
    cell.selectionStyle = UITableViewCellEditingStyleNone;
     return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}


@end
